<template>
  <div class='container'>
    <el-dialog
      :title="form.id?'修改学科' : '新增学科'"
      :visible="addSubjectDialogVisible"
      width="30%"
      @close="onClose">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="学科名称:" prop="subjectName">
            <el-input v-model="form.subjectName"></el-input>
          </el-form-item>
          <el-form-item label="是否显示:" >
            <el-switch
              v-model="form.isFrontDisplay"
              active-color="#13ce66"
              inactive-color="#ff4949"
              inactive-value="0"
              active-value="1"
              >
            </el-switch>
          </el-form-item>
        </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/subjects.js'
export default {
  name: 'addSubject',
  props: {
    addSubjectDialogVisible: {
      type: Boolean,
      required: true
    },
    form: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },

  computed: {

  },
  methods: {
    // 关闭弹层
    onClose () {
      this.$emit('update:addSubjectDialogVisible', false)
      this.$emit('on-close')
      this.$refs.form.resetFields()
    },
    // 点击确定
    async onSave () {
      await this.$refs.form.validate()
      if (this.form.id) {
        // 修改
        await update(this.form)
      } else {
        // 添加
        await add(this.form)
      }
      // 重新渲染列表
      this.$emit('on-save')
      // 关闭弹出层
      this.$emit('update:addSubjectDialogVisible', false)
      this.$message.success('操作成功')
    }
  }
}
</script>

<style scoped lang='less'></style>
